class Solution {
    TreeNode* preRoot = nullptr;
public:
    bool isValidBST(TreeNode* root) {
        if (!root) return true;
        bool left = isValidBST(root->left);
        if (preRoot && preRoot->val >= root->val) return false;
        preRoot = root;
        bool right = isValidBST(root->right);
        return left && right;
    }
};